/*
 * JTattoo LinuxStyleScrollBarSample (c) 2012 by MH Software-Entwicklung
 *
 * This sample shows how to set linux style scrollbars. In this case the increase/decrease buttons of scrollbars
 * are painted both on the right (horizontal scrollbar) or bottom (vertical scrollbar) side of the scrollbar.
 */

package com.surface.hao.sample;

import com.surface.hao.conf.CoreConf;
import com.surface.hao.frame.*;
import lombok.extern.log4j.Log4j;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.util.Properties;

/**
 * @Description
 *  工具箱启动主类
 * @Date 2019/12/29
 * @Author songhao
 **/
@Log4j
public class LinuxStyleScrollBarSample extends BaseSampleFrame {

    JScrollPane centerScrollPane = null;
    JPanel changePanel = null;
    JSplitPane splitPane = null;
    JScrollPane treeScrollPane = null;
    JTree jTree = null;

    public LinuxStyleScrollBarSample() {
        super("工具箱:" + CoreConf.userName);
        jTree = new JTree(getTreeNode());
        jTree.setFont(new Font("",Font.PLAIN,20));
        jTree.addTreeSelectionListener(new TreeSelectionListener() {

            public void valueChanged(TreeSelectionEvent e) {
                log.info("当前点击的菜单是:[" + e.getPath() + "] --- " + e.getNewLeadSelectionPath().getLastPathComponent());

                String menuName = e.getNewLeadSelectionPath().getLastPathComponent() + "";
                changeMenu(menuName);
            }
        });

        treeScrollPane = new JScrollPane(jTree);
        treeScrollPane.setMinimumSize(new Dimension(120, 80));
        //添加Panel
        changePanel = DateFormatPanel.getJpanel("日期与时间戳转换");
        centerScrollPane = new JScrollPane(changePanel);

        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, treeScrollPane, centerScrollPane);
        splitPane.setDividerLocation(180);

        contentPanel.add(splitPane, BorderLayout.CENTER);
    }

    /**
     * 根据菜单名称，进行菜单切换处理
     * @param menuName
     */
    private void changeMenu(String menuName) {
        if(menuName.equalsIgnoreCase("日期转换")){
            toDateChange();
        }else if(menuName.equalsIgnoreCase("文件名搜索")){
            toFileNameQuery();
        }else if(menuName.equalsIgnoreCase("内容查询")){
            toFileWordQuery();
        }else if(menuName.equalsIgnoreCase("小文件合并")){
            toFileMerge();
        }else if(menuName.equalsIgnoreCase("json格式化")){
            toJsonFormat();
        }
    }

    /**
     * 文件合并处理
     */
    private void toFileMerge() {
        log.info("切换到小文件合并菜单...");
        JPanel jPanel = FileMergePanel.getJpanel("小文件合并");
        refresh(jPanel);
    }

    /**
     * 文件内容查询
     */
    private void toFileWordQuery() {
        log.info("切换到内容查询菜单...");
        JPanel jPanel = WordSearchPanel.getJpanel("关键词搜索",this);
        refresh(jPanel);
    }

    /**
     * 文件名查询
     */
    private void toFileNameQuery() {
        log.info("切换到文件名查询菜单...");
        JPanel jPanel = FileSearchPanel.getJpanel("文件名搜索",this);
        refresh(jPanel);
    }

    /**
     * 字符串格式化
     */
    private void toJsonFormat(){
        log.info("字符串格式化...");
        JPanel jPanel = JsonFormatPanel.getJpanel("json字符串格式化",this);
        refresh(jPanel);
    }

    /**
     * 日期转换处理
     */
    private void toDateChange() {
        log.info("切换到日期转换菜单...");
        JPanel jPanel = DateFormatPanel.getJpanel("日期与时间戳转换");
        refresh(jPanel);
    }

    private void refresh(JPanel panel){
        //删除原有的面板
        contentSplitPane.remove(centerScrollPane);
        contentPanel.remove(splitPane);
        contentSplitPane.revalidate();
        contentSplitPane.validate();

        //添加新的Panel
        centerScrollPane = new JScrollPane(panel);
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, treeScrollPane, centerScrollPane);
        splitPane.setDividerLocation(180);
        contentPanel.add(splitPane, BorderLayout.CENTER);

        //显示窗口
        this.setVisible(true);
    }

    /**
     * 获取树形菜单信息
     * @return
     */
    private static DefaultMutableTreeNode getTreeNode(){
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("工具箱");
        DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("文件");
        DefaultMutableTreeNode child11 = new DefaultMutableTreeNode("文件名搜索");
        DefaultMutableTreeNode child12 = new DefaultMutableTreeNode("内容查询");
        DefaultMutableTreeNode child13 = new DefaultMutableTreeNode("小文件合并");

        DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("日期");
        DefaultMutableTreeNode child21 = new DefaultMutableTreeNode("日期转换");

        DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("json");
        DefaultMutableTreeNode child31 = new DefaultMutableTreeNode("json格式化");

        //添加第一级菜单
        root.add(child1);
        root.add(child2);
        root.add(child3);

        //添加第二级菜单
        child1.add(child11);
        child1.add(child12);
        child1.add(child13);
        child2.add(child21);
        child3.add(child31);
        return root;
    }

    @Override
    public Properties getLAFProps() {
        Properties props = super.getLAFProps();
        props.put("linuxStyleScrollBar", "on");
        return props;
    }
}
